package cn.rengy.web.framework.property.service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import cn.rengy.lang.BusinessException;
import cn.rengy.web.framework.cache.CacheService;
import cn.rengy.web.framework.cache.LocalCacheManager;
import cn.rengy.web.framework.sql.DbService;

@Service
public class PropertyService {
	private static Logger logger = LoggerFactory.getLogger(PropertyService.class);
	
	@Value("${spring.datasource.url}")
	private String datasource_url;
	@Autowired
	private DbService dbService;
	@Autowired
	private LocalCacheManager localCacheManager;
	@Autowired
	private CacheService cacheService;
	@Value("${spring.profiles.active}")
	private String profile;
	public String getProperty(String name,String defaultValue){
		try{
			return getProperty(name);
		}catch(Exception e){
			return defaultValue;
		}
	}
	
	public Long getProfileId() {
		if("local".equals(profile)) {
			return 21647509394817023L;
		}else if("test".equals(profile)) {
			return 21647509394817024L;
		}else if("dev".equals(profile)) {
			return 21647509394817025L;
		}else if("sit".equals(profile)) {
			return 21647509394817026L;
		}else if("prod".equals(profile)) {
			return 21647509394817027L;
		}
		return null;
	}
	
	//@Cacheable(value="sysPropertyCache",key="#name")
	public String getProperty(String name) {
		return cacheService.getLocalCache("sysPropertyCache", name, new Callable<String>() {
	        @Override
	        public String call() throws Exception {
	        	List<Map<String,Object>> propertyList= dbService.queryForList("select_sys_property_ByName", name);
				if(propertyList==null||propertyList.size()==0){
					throw new BusinessException("获取配置"+name+"为空!");
				}
				if(propertyList.size()==1){
					Map<String,Object> property=propertyList.get(0);
					return MapUtils.getString(property,"value");
				}else{
					for(int i=0;i<propertyList.size();i++){
						Map<String,Object> property=propertyList.get(i);
						String profile_=(String)property.get("profile");
						if(profile.equals(profile_)){
							return MapUtils.getString(property,"value");
						}
					}
				}
				return null;
	        }
	    });
	}
	
	public String getSchemaName(){
		Cache cache=localCacheManager.getCacheManager().getCache("sysPropertyCache");
		ValueWrapper valueWrapper=cache.get("schema_name");
		if(valueWrapper==null){
			int i=datasource_url.indexOf("?");
			int endIndex=i==-1?datasource_url.length():i;
			String schemaName=datasource_url.substring(datasource_url.lastIndexOf("/")+1,endIndex);
			cache.put("schema_name", schemaName);
			return schemaName;
		}else{
			return (String)valueWrapper.get();
		}
	}
	@CacheEvict(value="sysPropertyCache",key="#name")
	public int updateProperty(String name){
		
		return 1;
	}
	
}
